import * as usersService from '../services/users';

export default {
  namespace: 'users',
  state: {
    list: [],
    total: null,
    page: null,
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {
        if (pathname === '/users') {
          dispatch({
             type: 'getAll'
            });
        }
      });
    },
  },
  effects: {
    *getAll({payload},{call,put}){
      const data = yield call(usersService.getAll);
        console.log('111111',data);
        if(data){
          yield put({
            type:'save',
            payload:{list:data}
          })
        }
      },
    *delete({ payload }, { call, put }) {//删除批操作
        console.log('111111',payload);
        const { data } = yield call(usersService.remove,payload);
        if(data==0){
          message.success(`成功删除了${data}条数据！`);
        }else {
          message.error('操作失败，请重试！');
        }
        yield put({type:'getAll'})
      },

    // *remove({ payload: id }, { call, put, select }) {
    //   yield call(usersService.remove, id);
    //   const page = yield select(state => state.users.page);
    //   yield put({ type: 'fetch', payload: { page } });
    // },
  },
  reducers: {
        save(state, action) {
          return { ...state, ...action.payload};
      },
    },
};
